<template>
	<view class="container">
		<image class="icon" src="../../static/logo.png"></image>
		<text class="app_name">十里茶香</text>
		<button open-type="chooseAvatar" @chooseavatar="chooseAvatar" class="header-image"><image :src="userInfo.headimgurl"></image></button>
		<input class="input" placeholder="手机号" @input="chagePhone"/>
		<input class="input" password placeholder="密码" @input="chagePWD"/>
		<input class="input" placeholder="昵称" @input="chageName"/>
		<view class="btu">
			<button>取消</button>
			<button @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo:{
					"phone":"",
					"password":"",
					"headimgurl":"../../static/tabbar/s_my.png",
					"nickname":"",
					"openid":""
				}
			}
		},
		onLoad:function(option){
			console.log(option.openid)
			this.userInfo.openid=option.openid
		},
		methods: {
			chagePhone: function(event) {
			            this.userInfo.phone = event.target.value
			        },
			chagePWD:function(event) {
			            this.userInfo.password = event.target.value
			        },
			chageName:function(event) {
			            this.userInfo.nickname = event.target.value
			        },
			 chooseAvatar:function(e) {  
						this.userInfo.headimgurl=e.detail.avatarUrl
					},
			submit(){
				let that=this;
				if(this.userInfo.phone!=""){
					if(this.userInfo.nickname!=""){
						if(this.userInfo.password!=""){
							if(this.userInfo.headimgurl!=""){
								//修改
								
								uni.request({
									url:"http://localhost:8090/user/register",
									method:"POST",
									data:this.userInfo,
									success(res) {
										uni.showToast({
											title:res.data.message,
											icon:'success'
										})
										uni.setStorage({
											key: 'userInfo',
											data:that.userInfo,
											success: function() {
												uni.getStorage({
													key: 'userInfo',
													success: function (res) {
														console.log(res.data);
													}
												});
												uni.navigateBack();
											}
										});
									}
								})
							}else{
								uni.showToast({
									title:"请修改一下头像",
									icon:'error'
								})
							}
						}else{
							uni.showToast({
								title:"密码不能为空",
								icon:'error'
							})
						}
					}else{
						uni.showToast({
							title:"昵称不能为空",
							icon:'error'
						})
					}
				}else{
					uni.showToast({
						title:"手机号不能为空",
						icon:'error'
					})
				}
				console.log(this.userInfo);
			},
			
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.icon {
		width: 60px;
		height: 60px;
		margin-top: 30px;
	}

	.input {
		margin-top: 20px;
		height: 200px;
		height: 30px;
		border: 1px black solid;
	}

	.app_name {
		margin-top: 20px;
	}

	.btu {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 20px;
		width: 215px;
	}
	.header-image{
		width: 50px;
		height: 50px;
		border-radius: 50%; 
		padding: 0;
		margin-top: 30px;
	}
	.header-image>image{
		width: 50px;
		height: 50px;
		border-radius: 50%;
		margin: 0;
	}
</style>